﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Admin.Controllers
{
    public class RewardController : Controller
    {
        // GET: Admin/Reward
        public ActionResult Index(int? page, int? count, string CustomNo, string AuntName)
        {
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;
            AppSetting setting = new AppSetting();

            List<T_Reward> list = new List<T_Reward>();

            try
            {
                using (var db = new qianjingEntities())
                {
                    var query = db.T_Reward.AsQueryable();

                    //客户编码查询
                    if (!string.IsNullOrEmpty(CustomNo))
                    {
                        var Address = db.O_Address.FirstOrDefault(a => a.oasNo == CustomNo);
                        query = query.Where(a => a.trOCID == Address.oasOCID);
                    }
                    //管家名查询
                    if (!string.IsNullOrEmpty(AuntName))
                    {
                        var Aunt = db.O_Aunt.FirstOrDefault(a => a.oatName == AuntName);
                        string s = Aunt.OATID.ToString();
                        //query = query.Where(a => a.trOAID.Contains(Aunt.OATID.ToString()));
                        query = (from a in query
                                 where a.trOAID == s || a.trOAID.StartsWith(s + ",") || a.trOAID.EndsWith("," + s) || a.trOAID.Contains("," + s + ",")
                                 select a);
                    }
                    totalCount = query.Count();

                    var records = query.OrderByDescending(a => a.CreatedDate).Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();

                    if (records != null)
                    {

                        foreach (var item in records)
                        {
                            string strNo = "";
                            string strCustName = "";
                            string strAuntName = "";

                            var NoList = db.O_Address.Where(a => a.oasOCID == item.trOCID).ToList();
                            foreach (var i in NoList)
                            {
                                strNo += i.oasNo;
                                strCustName = i.oasName;
                            }

                            List<string> LiOaid = item.trOAID.Split(',').ToList();
                            foreach (var i in LiOaid)
                            {
                                var NameList = db.O_Aunt.Where(a => a.OATID.ToString() == i).FirstOrDefault();

                                strAuntName += NameList.oatName;
                            }



                            list.Add(new T_Reward
                            {
                                TRID = item.TRID,
                                trTotolRate = item.trTotolRate,
                                trOCID = item.trOCID,
                                trOAID = item.trOAID,
                                RowStatus = item.RowStatus,
                                CreatedDate = item.CreatedDate,
                                CreatedBy = strCustName,
                                CustomNo = strNo,
                                AuntName = strAuntName
                            });

                        }
                    }
                }

                ViewBag.TotalCount = totalCount;
                totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
                ViewBag.TotalPage = totalPage;
                ViewBag.CurrentPage = currentPage;
                ViewBag.PageCount = pageCount;
            }
            catch (Exception ex)
            {

            }
            return View(list);
        }
    }
}